<?php
  require_once('../includes.php');
  $membership = New Membership();
  $membership->confirm_admin();

  $message = false;
  $action  = $_POST['action'];
  $dag     = $_POST['dag'];
  $tijd    = $_POST['tijd'];
  $aantal  = trim($_POST['weken']);
  $agenda  = new Agenda();
  $week    = $agenda->week;
  $jaar    = $agenda->year; 
  $dag_nr  = Agenda::day_name_to_nr($dag);
  $nr      = Agenda::get_next_day_nr($dag_nr, $week, $jaar);

  $count_insertions = 0;
  $max_deelnemers   = Les::get_max_deelnemers();

  if ($action == 'create' || $action == 'edit')
  {
    for ($i = 1; $i <= $max_deelnemers; $i++)
    {
      $select = explode(' ', $_POST['keuze'.$i]);

      if ($select[0] != 'Persoon')
        $id[] = Website::un_format($select[0]);
    }
  }

  if ($action == 'add' || $action == 'del')
  {
    $lesweken = Agenda::create_week_array($_POST['from'], $_POST['to']);
    $select   = explode(' ', $_POST['person']);
    
    if ($select[0] != 'Selecteer')
      $id = $select[0];
  }

  /* Lessen aanmaken */
  if ($action == 'create')
  {
    $week  = $_POST['from'];
    $jaar  = $_POST['q_year'];
    $nr    = Agenda::get_next_day_nr($dag_nr, $week, $jaar);

    for ($i = 0; $i < $aantal; $i++)
    {
      mysql_query('INSERT INTO '.DB_LES.'(dag, week, jaar, tijd, dag_naam) VALUES ("'.$nr.'", "'.$week.'", "'.$jaar.'", "'.$tijd.'", "'.$dag.'")');

      foreach ($id as $user_id)
      {
        mysql_query('UPDATE leden SET vaste_klant=1 WHERE ID='.$user_id);
        mysql_query('INSERT INTO '.DB_DEELNEMER.' (les_ID, user_ID) VALUES (LAST_INSERT_ID(), "'.$user_id.'")');
      }

      $week  = Agenda::get_next_week_nr($week);
      $nr    = Agenda::get_next_day_nr($dag_nr, $week, $jaar);
      $jaar  = Agenda::get_next_year_nr($week, $jaar);
    }
  }

  /* Lessen verlengen */
  else if ($action == 'edit')
  {
    while ($count_insertions < $aantal)
    {
      $message = mysql_query('INSERT INTO '.DB_LES.'(dag, week, jaar, tijd, dag_naam) VALUES ("'.$nr.'", "'.$week.'", "'.$jaar.'", "'.$tijd.'", "'.$dag.'")');

      if ($message)
      {
        foreach ($id as $user_id)
        {
          mysql_query('UPDATE leden SET vaste_klant=1 WHERE ID='.$user_id);
          mysql_query('INSERT INTO '.DB_DEELNEMER.' (les_ID, user_ID) VALUES (LAST_INSERT_ID(), "'.$user_id.'")');
        }

        $count_insertions++;
      }
      
      $week  = Agenda::get_next_week_nr($week);
      $nr    = Agenda::get_next_day_nr($dag_nr, $week, $jaar);
      $jaar  = Agenda::get_next_year_nr($week, $jaar);
    }
  }

  /* Leden toevoegen aan meerdere lessen */
  else if ($action == 'add')
  { 
    mysql_query('UPDATE leden SET vaste_klant=1 WHERE ID='.$id);

    foreach ($lesweken as $lesweek)
    {
      $dag_nr = mysql_query('SELECT ID FROM '.DB_LES.' WHERE week="'.$lesweek.'" AND tijd="'.$tijd.'" AND dag_naam="'.$dag.'" LIMIT 1');

      if ($row = mysql_fetch_assoc($dag_nr))
      {
        $les_id = $row['ID'];
        $is_vol = mysql_query('SELECT COUNT(user_ID) FROM '.DB_DEELNEMER.' WHERE les_ID="'.$les_id.'"');
        $row2   = mysql_fetch_assoc($is_vol);

        if ($row2['COUNT(user_ID)'] < $max_deelnemers)
          $message = mysql_query('INSERT INTO '.DB_DEELNEMER.' (les_ID, user_ID) VALUES ("'.$les_id.'", "'.$id.'")');
      }
    }
  }

  /* Leden verwijderen uit meerdere lessen */
  else if ($action == 'del')
  { 
    mysql_query('UPDATE leden SET vaste_klant=0 WHERE ID='.$id);

    foreach ($lesweken as $lesweek)
    {
      $dag_nr = mysql_query('SELECT ID FROM '.DB_LES.' WHERE week="'.$lesweek.'" AND jaar="'.$jaar.'" AND tijd="'.$tijd.'" AND dag_naam="'.$dag.'" LIMIT 1');

      /* Ook kijken of er mensen op de reserve lijst staan : */
      if ($row = mysql_fetch_assoc($dag_nr))
      {
        $message = mysql_query('DELETE FROM '.DB_DEELNEMER.' WHERE les_ID="'.$row['ID'].'" AND user_ID="'.$id.'"');

        /* Haal het ID op van diegene die als eerst gereserveerd heeft */
        $reserve     = mysql_query('SELECT user_ID, MIN(counter) FROM '.DB_RESERVERING.' WHERE les_ID="'.$row['ID'].'" GROUP BY les_ID');
        $reserve_row = mysql_fetch_assoc($reserve);

        if ($reserve_row['MIN(counter)'] > 0)
          if (mysql_query('INSERT INTO '.DB_DEELNEMER.' (les_ID, user_ID) VALUES ("'.$row['ID'].'", "'.$reserve_row['user_ID'].'")'))
            mysql_query('DELETE FROM '.DB_RESERVERING.' WHERE les_ID="'.$row['ID'].'" AND user_ID="'.$reserve_row['user_ID'].'"');
      }
    }
  }

include('../header.php'); 
?>

<div id='agenda'>

<?php if ($message) : ?>
<div id='message'>
  <h3>Actie Geslaagd!</h3>
</div>
<?php endif; ?>

<div class="block">
  <h2 class="red">Lesgroepen</h2>
  <div id="info">
    <p>U kunt hier een optie kiezen:</p>
    <ul>
      <li><?php echo Website::create_link("admin/","Administratie"); ?><li>
      <li><?php echo Website::create_link("les/create.php", "Aanmaken", "add"); ?></li>
      <li><?php echo Website::create_link("les/select.php?a=add", "Leden Toevoegen aan meerdere bestaande lessen", "add-user"); ?></li>
      <li><?php echo Website::create_link("les/select.php?a=del", "Leden Verwijderen uit meerdere bestaande lessen", "del-user"); ?></li>
      <li><?php echo Website::create_link("les/select.php?a=edit", "Verlengen", "edit"); ?></li>
  </div>
</div>
</div>

<?php include('../footer.php'); ?>
